var express = require('express');
var router = express.Router();

var EventProxy = require('eventproxy'); /*避免回调金字塔*/
var $ = require('superagent'); /*客户端请求代理模块*/
var cache ={};// 缓存模板文件

var ModelProxy= require('modelproxy');
var mocker = require( 'mockjs' );
	var fs = require('fs');
	
/* GET home page. */
router.get('/newtask', function(req, res, next) {

	/*----------使用代理-------------*/
  	// 创建model
	// var userModel = new ModelProxy('User.getUserInfo');
	// // 使用model, 注意: 调用方法所需要的参数即为实际接口所需要的参数。
	// userModel.getUserInfo({id:"1"}).done(function(data){
	// 	res.render('test', data);
	// }).error(function(err){	
	// 	console.log("内部错误",err.message);
	// });
	
	/*------------前端数据模拟-------*/
	var rule = fs.readFileSync( './interfaceRules/User.getUserInfo.rule.json' );
	var schema = JSON.parse( rule );
	var data = mocker.mock(schema);
	
	res.render('newtask', {
		main:data,
		title:"新人任务页",
		layout:__dirname+'/layout/index',
		tasklist:[
		{
			img:"http://image.juxiangyou.com/task/1.png",
			intro:"10秒快速了解聚享游",
			jl:"1,000",
			link:"#",
			txt:"快速了解"
		},
		{
			img:"http://image.juxiangyou.com/web/img/newTask/tab_02.png",
			intro:"首次绑定手机认证",
			jl:"2,000",
			link:"#",
			txt:"我要绑定"
		},
		{
			img:"http://image.juxiangyou.com/task/3.jpg",
			intro:"领取任意试玩第一步奖励",
			jl:"3,000",
			link:"#",
			txt:"立即试玩"
		}
		]
	});
	
	// res.render('test', data,function(err,html){
	// 	console.log("html:"+html);
 //        res.write(html);
	// })

});

router.get('/',function(req,res){

	var proxy = new EventProxy();
	proxy.all('games_rqueset', function (data1, data2) {
	  console.log("require all end");
	  res.end(); //结束请求
	});

	//res.render('indexpage');
	res.render('index',{},function(err,html){
			console.log(html);
		res.writeHead(200,{'Content-Type':'text/html',"chartset":"utf-8"});
		res.write(html);

		var proxy = new EventProxy();
		/* 注册该页面所有的数据请求 完成所有请求后才调用*/
		proxy.all('games_event','news_event','awards_event','reward_event', function (data1, data2) {
		  console.log("require all end");
		  res.end(); // 结束请求
	});

		/* 异步获取数据请求 先获取先渲染到页面 */

		_fecthData('./interfaceRules/Game.getGameInfo.rule.json',function(data){
			res.render('./basic/gamesitem',data,function(err,html){
				console.log("games_event");
				res.write("<script>bigpipe.set('hotgames',"+JSON.stringify(html)+")</script>");
				proxy.emit('games_event', html);
			})
		});
		

		_fecthData('./interfaceRules/News.getNewsInfo.rule.json',function(data){
			res.render('./basic/newsitem',data,function(err,html){
				console.log("news_event");
				res.write("<script>bigpipe.set('hotnews',"+JSON.stringify(html)+")</script>");
				proxy.emit('news_event', html);
			})
	});

		_fecthData('./interfaceRules/Award.getAwardnfo.rule.json',function(data){
			res.render('./basic/awardsitem',data,function(err,html){
				console.log("awards_event");
				res.write("<script>bigpipe.set('hotawards',"+JSON.stringify(html)+")</script>");
				proxy.emit('awards_event', html);
			})
		});

		_fecthData('./interfaceRules/Reward.getRewardnfo.rule.json',function(data){
			res.render('./basic/exchangeitem',data,function(err,html){
				console.log("reward_event");
				res.write("<script>bigpipe.set('hotreward',"+JSON.stringify(html)+")</script>");
				proxy.emit('reward_event', html);
			})
		});
	})
});


function _fecthData(url,callback){
	var rule = fs.readFileSync(url);
	var schema = JSON.parse( rule );
	var data = mocker.mock(schema);
	if(callback && typeof callback ==="function"){
		callback.call(this,data);
	}
}

function getTpl(name){
	if(name && name!=""){
		if(!cache[name]){
      		cache[name] = fs.readFileSync("./views/" + name,'utf-8');
    	}
    	return cache[name];
	}
	else{
		return "";
	}
	
}




module.exports = router;
